package cn.sinata.wanglin.adapter;


import android.view.View;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.mine.activity.MyAreaActivity;
import cn.sinata.wanglin.model.MyVillage;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.RxBus;

public class MyAreaAdapter extends HFRecyclerAdapter<MyVillage> {

    public MyAreaAdapter(List<MyVillage> mData) {
        super(mData, R.layout.item_myarea);
    }

    @Override
    public void onBind(final int position, final MyVillage data, ViewHolder holder) {
        holder.setText(R.id.tv_villagename, data.getHousingName());
        holder.setText(R.id.tv_address, data.getBuilding() + "栋" + data.getUnit() + "单元" + data.getFloor() + "楼" + data.getDoorNumber() + "号");
        if ("0".equals(data.getOwner_type())) {
            //业主
            holder.setText(R.id.tv_owertype, "业主");
        } else {
            holder.setText(R.id.tv_owertype, "租客");
        }
        if ("0".equals(data.getIsDefault())) {
            //是默认
            holder.bind(R.id.iv_isdefult).setVisibility(View.VISIBLE);
            holder.bind(R.id.tv_defult).setVisibility(View.GONE);
        } else {
            holder.bind(R.id.iv_isdefult).setVisibility(View.GONE);
            holder.bind(R.id.tv_defult).setVisibility(View.VISIBLE);
        }
        holder.bind(R.id.tv_defult).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                defaultHousing(position);
            }
        });
        holder.bind(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                delHousing(position);
            }
        });

    }

    private void defaultHousing(final int position) {
        showDialog();
        String userid = SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.defaultHousing(userid, mData.get(position).getId())
                .subscribe(new ResultDataSubscriber<JsonObject>((BaseActivity) mContext) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        SharedPreferencesUtils.save(Constant.SP_HOUSING_ID, mData.get(position).getHousingId());
                        SharedPreferencesUtils.save(Constant.SP_HOUSING_NAME, mData.get(position).getHousingName());
                        ((MyAreaActivity) mContext).refresh();
                    }
                });
    }

    private void delHousing(final int position) {
        showDialog();
        final MyVillage village = mData.get(position);
        String userid = SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.delHousing(village.getId(), userid)
                .subscribe(new ResultDataSubscriber<JsonObject>((BaseActivity) mContext) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        String s = village.getId();
                        RxBus.get().post(Constant.DELETE_VILLAGE_ACTION, s);
                        mData.remove(village);
                        notifyDataSetChanged();
                    }
                });
    }
}
